﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;


namespace MyLib
{
    // Actually it is not a 'storage account'
    // it is a composition of storage and deploymentID,
    // so we can get the info: which DiagnosticMonitors are started AND configured 
    // for this Service (deploymentID) and this Storage
    public class clsStorageAccount
    {
        protected const string AccountName = "accountName";
        protected const string AccountKey = "accountKey";
        protected const string DeploymentId = "deploymentId";

        public string StorageName { get; set; }
        public string StorageKey { get; set; }
        public string DeploymentID { get; set; }

        //private constructor. Use the static method GetStorageAccount() to get the new object
        private clsStorageAccount()
        { 
        }

        public void Save()
        {
            HttpResponse response = HttpContext.Current.Response;

            response.Cookies.Add(GetCookie(clsStorageAccount.AccountName, StorageName));
            response.Cookies.Add(GetCookie(clsStorageAccount.AccountKey, StorageKey));
            response.Cookies.Add(GetCookie(clsStorageAccount.DeploymentId, DeploymentID));
            
            _isAccountValid = null;
 
        }

        private HttpCookie GetCookie(string sName, string sValue)
        {
            HttpCookie aCookie = new HttpCookie(sName, sValue);
            aCookie.Expires = DateTime.Now.AddMonths(1);
            return aCookie;
        }

        // check if the account info is valid
        private bool? _isAccountValid = null;
        public bool IsAccountValid
        {
            get
            {
                if (_isAccountValid != null)
                {
                    return (bool)_isAccountValid; 
                }

                _isAccountValid = true;

                if (StorageName == "" || StorageKey == "")
                {
                    _isAccountValid = false;
                }
                else
                {
                    try
                    {
                        var storageCredentialsAccountAndKey = new StorageCredentialsAccountAndKey(StorageName, StorageKey);
                        CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(storageCredentialsAccountAndKey, true);
                        var blobClient = cloudStorageAccount.CreateCloudBlobClient();
                        var list = blobClient.ListContainers();
                    }
                    catch (Exception ex)
                    {
                        _isAccountValid = false;
                    }
                }

                return (bool)_isAccountValid;
            }
        }


        // TODO Cookies are not available after closing of Browser. Investigate why and fix it
        internal static  clsStorageAccount GetStorageAccount()
        {
            HttpRequest httpRequest = HttpContext.Current.Request;
            var sa = new clsStorageAccount();
            string s = "";

            s = "";
            var aCookie = httpRequest.Cookies[clsStorageAccount.AccountName];
            if (aCookie != null) { s = aCookie.Value; } 
            sa.StorageName = s;

            s = "";
            aCookie = httpRequest.Cookies[clsStorageAccount.AccountKey];
            if (aCookie != null) { s = aCookie.Value; } 
            sa.StorageKey = s;

            s = "";
            aCookie = httpRequest.Cookies[clsStorageAccount.DeploymentId];
            if (aCookie != null) { s = aCookie.Value; }
            sa.DeploymentID = s;

            return sa;

        }

        public static void SetExpired()        
        {           

            SetExpiredItem(clsStorageAccount.AccountName);
            SetExpiredItem(clsStorageAccount.AccountKey);
            SetExpiredItem(clsStorageAccount.DeploymentId);
        }

        private static void SetExpiredItem(string ItemName)
        {
            HttpRequest httpRequest = HttpContext.Current.Request;

            var aCookie = httpRequest.Cookies[ItemName];
            if (aCookie == null)
            {
                return;
            }

            aCookie.Expires = DateTime.Now.AddHours(-1d);
            HttpContext.Current.Response.Cookies.Add(aCookie);

        }


        public static bool StaticIsAccountValid()
        {
            var sa = clsStorageAccount.GetStorageAccount();
            bool b = sa.IsAccountValid;
            return b;
        }
    }
}